<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="分类名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入分类名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="父分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parentCid">
              <j-tree-select
                v-model="model.parentCid"
                placeholder="请选择菜单"
                dict="pms_category,name,cat_id"
                pidField="parent_cid"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图标" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="icon">
              <!--              <a-input v-model="model.icon" placeholder="请选择图标"  ></a-input>-->
              <a-input placeholder="点击选择图标" v-model="model.icon">
                <a-icon slot="addonAfter" type="setting" @click="selectIcons"/>
              </a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productUnit">
              <a-input v-model="model.productUnit" placeholder="请输入商品单位"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productCount">
              <a-input-number v-model="model.productCount" placeholder="请输入商品数量" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>

    <!-- 选择图标 -->
    <icons @choose="handleIconChoose" @close="handleIconCancel" :iconChooseVisible="iconChooseVisible"></icons>
  </a-spin>
</template>

<script>

import {httpAction, getAction} from '@/api/manage'
import {validateDuplicateValue} from '@/utils/util'
import Icons from "@views/system/modules/icon/Icons";

export default {
  name: 'CategoryForm',
  components: {
    Icons
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      confirmLoading: false,
      validatorRules: {},
      iconChooseVisible: false,
      url: {
        add: "/category/pmsCategory/add",
        edit: "/category/pmsCategory/edit",
        queryById: "/category/pmsCategory/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add(parentCid) {
      if (parentCid) {
        this.modelDefault.parentCid = parentCid[0] + ""
      }
      this.edit(this.modelDefault);
    },

    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },

    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },

    selectIcons() {
      this.iconChooseVisible = true
    },
    handleIconCancel() {
      this.iconChooseVisible = false
    },
    handleIconChoose(value) {
      console.log(value)
      this.model.icon = value
      this.iconChooseVisible = false
    },
  }
}
</script>